package com.authManager.server.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.authManager.entity.SysUserRole;
import com.authManager.server.service.ISysUserRoleService;
import com.commons.base.model.AdminPage;
import com.commons.base.model.ResultCode;
import com.commons.base.model.ResultData;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 【用户角色关联表】 前端控制器
 * </p>
 *
 * @author 小旋风
 * @since 2018-8-17
 */

@Api(value = "/admin/sysUserRole", tags = "【用户角色关联表】操作")
@RestController
@Validated
@RequestMapping(value = "/admin/sysUserRole", produces = BaseController.defProduces)
public class SysUserRoleController extends BaseController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(SysUserRoleController.class);
	/*
	 * 【1】获取单个对象的方法用 get 做前缀。 【2】获取多个对象的方法用 list 做前缀。 【3】获取统计值的方法用 count 做nz前缀。
	 * 【4】插入的方法用 save（ 推荐 ） 或 insert 做前缀。 【5】删除的方法用 remove（ 推荐 ） 或 delete 做前缀。
	 * 【6】修改的方法用 update 做前缀。
	 */

	@Autowired
	ISysUserRoleService sysUserRoleService;

	@ApiOperation(value = "添加或者删除【用户角色关联表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "sysUserId", value = "用户id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysRoleId", value = "角色id", required = false), })
	@RequestMapping(value = "addOrDelData", method = { RequestMethod.POST })
	public ResultData<Object> addOrDelData(@RequestParam(required = true) Long sysRoleId,
			@RequestParam(required = true) String sysUserId) {
		return sysUserRoleService.addOrDelData(sysRoleId, sysUserId,getCurrUserLoginId());
	}

	@ApiOperation(value = "修改【用户角色关联表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysUserRoleIdStr", value = "主键id", required = true),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "sysUserId", value = "用户id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysRoleId", value = "角色id", required = false), })
	@RequestMapping(value = "editSysUserRole", method = { RequestMethod.POST })
	public ResultData<Object> editSysUserRole(@RequestParam(required = true) Long sysUserRoleId,
			@RequestParam(required = false) String sysUserId, @RequestParam(required = false) Long sysRoleId) {
		SysUserRole sysUserRole = sysUserRoleService.selectById(sysUserRoleId);
		if (sysUserRole == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		sysUserRole = new SysUserRole();
		if (sysUserRoleId != null) {
			sysUserRole.setSysUserRoleId(sysUserRoleId);
		}
		if (StringUtils.isNotBlank(sysUserId)) {
			sysUserRole.setSysUserId(sysUserId);
		}
		if (sysRoleId != null) {
			sysUserRole.setSysRoleId(sysRoleId);
		}
		// sysUserRole.setCreater(null);
		// sysUserRole.setCreateTime(null);
		sysUserRoleService.updateById(sysUserRole);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "删除【用户角色关联表】")
	@RequestMapping(value = "delSysUserRole", method = { RequestMethod.POST })
	public ResultData<Object> delSysUserRole(@RequestParam(required = true) Long sysUserRoleId) {
		SysUserRole sysUserRole = sysUserRoleService.selectById(sysUserRoleId);
		if (sysUserRole == null) {
			return new ResultData.Builder<Object>(ResultCode.isNull).build();
		}
		// 自己根据需求放开注释吧
		// 物理删除
		// sysUserRoleService.deleteById(sysUserRoleId);
		// 逻辑删除
		// sysUserRole.setStatus(0);
		// sysUserRoleService.updateById(sysUserRoleId);
		return new ResultData.Builder<Object>(ResultCode.success).build();
	}

	@ApiOperation(value = "获取【用户角色关联表】")
	@RequestMapping(value = "getSysUserRole", method = { RequestMethod.POST })
	public ResultData<SysUserRole> getSysUserRole(@RequestParam(required = true) Long sysUserRoleId) {
		SysUserRole sysUserRole = sysUserRoleService.selectById(sysUserRoleId);
		if (sysUserRole == null) {
			return new ResultData.Builder<SysUserRole>(ResultCode.isNull).build();
		}
		return new ResultData.Builder<SysUserRole>(ResultCode.success).setData(sysUserRole).build();
	}

	@ApiOperation(value = "列表【用户角色关联表】")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageNum", value = "页码", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "pageSize", value = "每页大小", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysUserRoleId", value = "主键id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "sysUserId", value = "用户id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "Long", name = "sysRoleId", value = "角色id", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "start_createTime", value = "开始-创建时间", required = false),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "end_createTime", value = "截至-创建时间", required = false), })
	@RequestMapping(value = "listSysUserRole", method = { RequestMethod.POST })
	public ResultData<AdminPage<SysUserRole>> listSysUserRole(
			@RequestParam(defaultValue = "1", required = false) Integer pageNum,
			@RequestParam(defaultValue = "10", required = false) Integer pageSize,
			@RequestParam(required = false) Long sysUserRoleId, @RequestParam(required = false) String sysUserId,
			@RequestParam(required = false) Long sysRoleId, @RequestParam(required = false) String start_createTime,
			@RequestParam(required = false) String end_createTime) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pageNum", pageNum);
		paramMap.put("pageSize", pageSize);
		// 主键id
		if (sysUserRoleId != null) {
			paramMap.put("sysUserRoleId", sysUserRoleId);
		}
		// 用户id
		if (StringUtils.isNotBlank(sysUserId)) {
			paramMap.put("sysUserId", sysUserId);
		}
		// 角色id
		if (sysRoleId != null) {
			paramMap.put("sysRoleId", sysRoleId);
		}
		// 开始-创建时间
		if (StringUtils.isNotBlank(start_createTime)) {
			paramMap.put("start_createTime", start_createTime);
		}
		// 结束-创建时间
		if (StringUtils.isNotBlank(end_createTime)) {
			paramMap.put("end_createTime", end_createTime);
		}
		return sysUserRoleService.listSysUserRole(paramMap);
	}
}
